package test.my.com.myapplicationtest.horizontal_zhuxingtu;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import test.my.com.myapplicationtest.R;


/**
 * Author : cooper
 * Time :  2018/3/20 下午3:16
 * Description : 横向柱形图
 */

public class HorizontalCylindricalView extends View {

    private Context mContext;

    private int mWidth = 100;
    private int mHeight = 30;


    private Paint mZPaint;
    private Paint mXPaint;
    private Paint mWPaint;

    private RectF mRectF;
    private RectF mValueRectF;

    private int mSpace = 4;
    private Path mXPath1 = new Path();
    private Path mXPath2 = new Path();
    private Path mXPath3 = new Path();

    private Rect mTextRect1 = new Rect();
    private Rect mTextRect2 = new Rect();
    private Rect mTextRect3 = new Rect();

    private String oneStr;
    private String twoStr;
    private String threeStr;

    private int mTotalNum = 1;
    private int mValue = 0;

    // 横柱形图距离上部距离
    private int mCVMarginTop;
    // 横柱形图距离右边距离
    private int mCVMargeRight;
    // 柱形图高度
    private int mCVheight;
    // 文字距离柱形图高度
    private int mCVTextTop;
    // 文字大小
    private int mCVTextSize;

    private boolean mIsShowXuXian;



    public HorizontalCylindricalView(Context context) {
        super(context);
    }

    public HorizontalCylindricalView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public HorizontalCylindricalView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.HorizontalCylindricalView,defStyleAttr,0);
        mCVMarginTop = ta.getDimensionPixelOffset(R.styleable.HorizontalCylindricalView_cVMarginTop,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,
                getResources().getDisplayMetrics()));
        mCVMargeRight = ta.getDimensionPixelOffset(R.styleable.HorizontalCylindricalView_cVMarginRight,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,
                getResources().getDisplayMetrics()));
        mCVMarginTop = ta.getDimensionPixelOffset(R.styleable.HorizontalCylindricalView_cVMarginTop,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12,
                getResources().getDisplayMetrics()));
        mCVheight = ta.getDimensionPixelOffset(R.styleable.HorizontalCylindricalView_cVheight,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,
                getResources().getDisplayMetrics()));
        mCVTextTop = ta.getDimensionPixelOffset(R.styleable.HorizontalCylindricalView_cVTextTop,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,
                getResources().getDisplayMetrics()));
        mCVTextSize = ta.getDimensionPixelOffset(R.styleable.HorizontalCylindricalView_cVTextSize,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,
                getResources().getDisplayMetrics()));
        ta.recycle();

        init();
    }

    private void init() {
        mZPaint = new Paint();
        mZPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mZPaint.setColor(Color.parseColor("#F01C6D"));
        mZPaint.setStyle(Paint.Style.FILL);

        mXPaint = new Paint();
        mXPaint.setAntiAlias(true);
        mXPaint.setStyle(Paint.Style.STROKE);
        mXPaint.setStrokeWidth(2);
        mXPaint.setPathEffect(new DashPathEffect(new float[]{10,8}, 0));

        mWPaint = new Paint();
        mWPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mWPaint.setColor(Color.parseColor("#BEC7CE"));
        mWPaint.setTextSize(mCVTextSize);

    }

    public void setValue(int totalNum, int value,boolean isShowXuXian) {
        this.mTotalNum = totalNum;
        this.mValue = value;
        this.mIsShowXuXian = isShowXuXian;
        oneStr = totalNum * 2 / 10 + "";
        twoStr = totalNum * 8 / 10 + "";
        threeStr = totalNum + "";
        mWPaint.getTextBounds(oneStr,0,oneStr.length(),mTextRect1);
        mWPaint.getTextBounds(twoStr,0,twoStr.length(),mTextRect2);
        mWPaint.getTextBounds(threeStr,0,threeStr.length(),mTextRect3);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawXuXian(canvas);
        drawPercentRect(canvas);
        drawText(canvas);
    }

    private void drawXuXian(Canvas canvas) {
        if (!mIsShowXuXian) {
            return;
        }
        mXPaint.setColor(Color.parseColor("#F01C6D"));
        mXPath1.moveTo((mWidth-mSpace-mCVMargeRight)*2/10,mSpace);
        mXPath1.lineTo((mWidth-mSpace-mCVMargeRight)*2/10,mHeight-mSpace);
        canvas.drawPath(mXPath1,mXPaint);

        mXPaint.setColor(Color.parseColor("#F69103"));
        mXPath2.moveTo((mWidth-mSpace-mCVMargeRight)*8/10,mSpace);
        mXPath2.lineTo((mWidth-mSpace-mCVMargeRight)*8/10,mHeight-mSpace);
        canvas.drawPath(mXPath2,mXPaint);

        mXPaint.setColor(Color.parseColor("#3778F2"));
        mXPath3.moveTo((mWidth-mSpace-1-mCVMargeRight),mSpace);
        mXPath3.lineTo((mWidth-mSpace-1-mCVMargeRight),mHeight-mSpace);
        canvas.drawPath(mXPath3,mXPaint);
    }

    private void drawPercentRect(Canvas canvas) {
        if (!mIsShowXuXian) {
            mZPaint.setColor(Color.parseColor("#EEF2FA"));
            canvas.drawRect(mRectF,mZPaint);
        }
        if (mValue < mTotalNum * 2 / 10) {
            mZPaint.setColor(Color.parseColor("#F01C6D"));
        } else if (mValue > mTotalNum * 2 / 10 && mValue < mTotalNum * 8 / 10) {
            mZPaint.setColor(Color.parseColor("#F69103"));
        } else if (mValue > mTotalNum * 8 / 10 && mValue < mTotalNum) {
            mZPaint.setColor(Color.parseColor("#3778F2"));
        }
        mValueRectF = new RectF(mRectF.left, mRectF.top, mRectF.right * mValue / mTotalNum, mRectF.bottom);
        canvas.drawRect(mValueRectF,mZPaint);
    }

    /**
     * 添加文字
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        if (mIsShowXuXian) {
            return;
        }
        canvas.drawText(oneStr,mRectF.width()*2/10 - (mTextRect1.width()/2),mSpace+mRectF.bottom+mCVTextTop,mWPaint);
        canvas.drawText(twoStr,mRectF.width()*8/10 - (mTextRect2.width()/2),mSpace+mRectF.bottom+mCVTextTop,mWPaint);
        float dc = 0f;
        if (mRectF.right + (mTextRect3.width() / 2) > mWidth) {
//            dc = mRectF.right + (mTextRect3.width() / 2) - mWidth;
            dc = mRectF.width() - mTextRect3.width() - mRectF.right +mWidth;;
        } else {
            dc = mRectF.width() - (mTextRect3.width()/2);
        }
        canvas.drawText(threeStr,dc,mSpace+mRectF.bottom+mCVTextTop,mWPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
        mRectF = new RectF(mSpace,mSpace+mCVMarginTop,mWidth-mSpace-mCVMargeRight,mCVheight+mCVMarginTop);
    }

    private int measureWidth(int widthMeasureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        } else {
            result = mWidth;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result, specSize);
            }
        }
        this.mWidth = result;
        return result;
    }

    private int measureHeight(int heightMeasureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        } else {
            result = mHeight;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result, specSize);
            }
        }
        this.mHeight = result;
        return result;
    }


}
